package models

import (
	"gorm.io/gorm"
	"user-srv/basic/config"
)

type Shop struct {
	gorm.Model
	Img        string  `gorm:"type:varchar(200);comment:'砍价图片'"`
	Title      string  `gorm:"type:varchar(30);comment:'砍价名称'"`
	Price      float64 `gorm:"type:decimal(10,2);comment:'砍价价格'"`
	LowPrice   float64 `gorm:"type:decimal(10,2);comment:'最低加'"`
	Num        int     `gorm:"type:int;comment:'参加人数'"`
	PriceNum   int     `gorm:"type:int;comment:'帮忙砍价人数'"`
	SuccessNum int     `gorm:"type:int;comment:'砍价成功人数'"`
	Limit      int     `gorm:"type:int;comment:'限量'"`
	Remaining  int     `gorm:"type:int;comment:'限量剩余'"`
	Status     int     `gorm:"type:int;default:1;comment:'商品状态: 1上架 2:下架'"`
}

func (s *Shop) TableName() string {
	return "shop"
}

// 查看商品id
func (s *Shop) GetShopById(shopId int) error {
	return config.DB.Where("id = ?", shopId).Limit(1).Find(&s).Error
}

// 添加商品
func (s *Shop) AddShop() error {
	return config.DB.Create(&s).Error
}

// 商品展示
func (s *Shop) ListShop(page, pageSize int, title string) (shops []Shop, err error) {
	db := config.DB.Model(&Shop{})
	offset := (page - 1) * pageSize
	if s.Title != "" {
		db = db.Where("title like ?", "%"+title+"%")
	}
	if page > 0 || pageSize < 0 {
		db = db.Offset(offset).Limit(pageSize)
	}
	db.Find(&shops)
	return shops, err

}

// 商品修改
func (s *Shop) UpdateShop(shopId int) error {
	return config.DB.Where("id = ?", shopId).Updates(&s).Find(&s).Error
}
